package com.hb.software.controller.register;

import com.alibaba.fastjson.JSON;
import com.hb.software.domain.Result;
import com.hb.software.entity.user.User;
import com.hb.software.entity.worker.Worker;
import com.hb.software.register.RegisterService;
import com.hb.software.user.UserService;
import com.hb.software.utils.CreateImageCode;
import com.hb.software.worker.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("register")
@CrossOrigin //允许跨域
@Slf4j
public class RegisterController {

    private static final Logger logger = LogManager.getLogger(RegisterController.class.getName());

    @Autowired
    private RegisterService registerService;

    @Autowired
    private WorkerService workerService;

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public void register(@RequestBody User user){
        logger.info(user);
        Long id = workerService.findLastId();
        user.setId(id+1);
        registerService.register(user);
    }
    @PostMapping("/updateRole")
    public void updateRole(@RequestBody String num){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String adminNum = registerService.selectAdminnum();
        if(num.substring(0,num.length()-1).equals(adminNum)) {
            User user = userService.findById(workerService.findLastId());
            logger.info(user);
            userService.updateRole(user);
        }
    }

    /**
     * 生成验证码
     *
     * @throws IOException
     */
    @GetMapping("getImage")
    public Map<String, String> getImage(HttpServletRequest request) throws IOException {
        Map<String, String> result = new HashMap<>();
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String securityCode = createImageCode.getCode();
        //验证码存入session
        String key = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        request.getServletContext().setAttribute(key, securityCode);
        //生成图片
        BufferedImage image = createImageCode.getBuffImg();
        //进行base64编码
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", bos);
        String string = Base64Utils.encodeToString(bos.toByteArray());
        result.put("key", key);
        result.put("image", string);
        result.put("true_code", securityCode);
        logger.info(securityCode);
        return result;
    }

    @PostMapping("/selectAdminnum")
    public String selectAdminnum(){
        String str = registerService.selectAdminnum();
        logger.info("管理员注册码为"+str);
        HashMap<String,Object> res = new HashMap<>();
        res.put("adminnum",str);
        String res_json = JSON.toJSONString(res);
        return res_json;
    }

    @PostMapping("/updateAdminnum")
    public void updateAdminnum(@RequestBody String str){
         registerService.updateAdminnum(str.substring(0,str.length()-1));
        logger.info("管理员注册码修改为"+str.substring(0,str.length()-1));
    }

}
